﻿/*
	* Project	: SharePoint Dynamic Forms
	* Version	: 1.0
	* Company	: SharePoint Frontier Software Pvt Ltd. (http://www.sharepointfrontier.com)
	* Author	: Ashok Raja .T (Blog : http://www.ashokraja.me)
	* Licence	: Published under Microsoft Public License (Ms-PL)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFS.SharePoint.DynamicForms.Validation
{
    public class RangeValidator
    {
        public static bool Between(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            return Validate(rule, Field);
        }

        private static bool Validate(SPDynamicFieldValidationRule rule, SPDynamicField Field)
        {
            try
            {
                string[] NumericData = rule.ValidateAgainst.Split('|');
                string ErrorMsg = string.Empty;
                SPDynamicFieldValidationRule rl = new SPDynamicFieldValidationRule();
                rule.ValidateAgainst = "";
                bool isValidData = true;
                string ValMsg = string.Empty;
                bool isInvalidRule = false;
                if (Field.FieldType == "DateTime")
                {
                    rl.ValidateAgainst = NumericData[0];
                    if (!DateValidator.GreaterThanEqual(rl, Field))
                    {
                        isValidData = false;
                        ValMsg = rl.ValidationMessage;
                        isInvalidRule = rl.IsInvalidRule;
                    }

                    rl.ValidateAgainst = NumericData[1];
                    if (!DateValidator.LessThanEqual(rl, Field))
                    {
                        isValidData = false;
                        ValMsg += "  |  " + rl.ValidationMessage;
                        if (rl.IsInvalidRule)
                            isInvalidRule = rl.IsInvalidRule;
                    }

                    if (!isValidData)
                    {
                        rule.IsValid = false;
                        if (isInvalidRule)
                            rule.ValidationMessage = rule.ValidationType + " validation rule is invalid for " + Field.DisplayName + ".";
                        else
                            rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Value must be between " + ValidationHelper.GetDateFromTokenAsString(NumericData[0]) + " and " + ValidationHelper.GetDateFromTokenAsString(NumericData[1]) + ".");
                        return false;
                    }
                }
                else
                {
                    rl.ValidateAgainst = NumericData[0];
                    if (!NumericValidator.GreaterThanEqual(rl, Field))
                    {
                        isValidData = false;
                        isInvalidRule = rl.IsInvalidRule;
                    }

                    rl.ValidateAgainst = NumericData[1];
                    if (!NumericValidator.LessThanEqual(rl, Field))
                    {
                        isValidData = false;
                        if (rl.IsInvalidRule)
                            isInvalidRule = rl.IsInvalidRule;
                    }

                    if (!isValidData)
                    {
                        rule.IsValid = false;
                        if (isInvalidRule)
                            rule.ValidationMessage = rule.ValidationType + " validation rule is invalid for " + Field.DisplayName + ".";
                        rule.ValidationMessage = ValidationHelper.ProcessErrorMsg(Field, rule, "Value must be between " + NumericData[0] + " and " + NumericData[1] + ".");
                        return false;
                    }
                }

                return true;
            }
            catch
            {
                rule.IsValid = false;
                rule.ValidationMessage = "Unable to validate range validation.";
                return false;
            }
        }

        //private SPDynamicFieldValidationRule GetRule(SPDynamicFieldValidationRule rule, string Value)
        //{ 
        // SPDynamicFieldValidationRule r = new SPDynamicFieldValidationRule();
        // r.ValidateAgainst = Value;
        // r.ValidationMessage = rule.ValidationMessage;
        //    r.ValidationType
        //}

    }
}
